home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17553 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  66 lines

  1. Path: brighton.openmarket.com!decwrl!sony!sonysjc!usenet
  2. From: Michael Rizzo <rizzom@mars.superlink.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Saving the contents of an object to a file
  5. Date: Tue, 16 Apr 1996 09:15:47 -0400
  6. Organization: Sony Corporation of America, San Jose, CA
  7. Message-ID: <31739D83.5F61@mars.superlink.net>
  8. NNTP-Posting-Host: rizzom.rmpg.sel.sony.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hi,
  15.  
  16.     I am having a problem saving the contents of an object to a 
  17. file.  For instance, I have the following code:
  18.  
  19. class A
  20. {
  21. private:
  22.     int id;
  23.     char *name;
  24. public:
  25.     A(int, char*);
  26.     A();
  27.     ~A();
  28.     char * GetName() {return name;}
  29.     int GetId() {return id;}
  30.  
  31. };
  32.  
  33. A::A(int i, char *n)
  34. {
  35.     int temp;
  36.     id = i;
  37.     temp = strlen(n);
  38.     name = new char[temp+1];
  39.     strcpy(name,n);
  40. }
  41.  
  42. A::A()
  43. {
  44.     id = 0;
  45.     name = new char[5];
  46.     strcpy(name, "none");
  47. }    
  48.  
  49. int main()
  50. {
  51.     A x(10,"JOE"), y;
  52.     ofstream outf("savefile",ios::binary);
  53.     outf.write((char*) &x, sizeof x);
  54.     outf.write((char*) &y, sizeof y);
  55.     outf.close();
  56.     return 0;
  57. }
  58.  
  59. This produces a file "savefile" with size 0.  What am I doing wrong?  
  60. BTW, I am using DJGPP v2 to compile this.  
  61.  
  62. Any help would be greatly appreciated.
  63.  
  64. Mike
  65. rizzom@mars.superlink.net
  66.